package com.ruoyi.framework.session;

import com.ruoyi.common.utils.AddressUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.project.monitor.online.domain.UserOnline;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.mapper.UserMapper;
import eu.bitwalker.useragentutils.UserAgent;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 待完善
 *
 * @author bootdo
 */
@Service
public class SessionServiceImpl implements SessionService {
  private final SessionDAO sessionDAO;

  @Autowired
  private UserMapper userMapper;

  @Autowired
  public SessionServiceImpl(SessionDAO sessionDAO) {
    this.sessionDAO = sessionDAO;
  }

  @Override
  public List<UserOnline> list() {
    List<UserOnline> list = new ArrayList<>();
    Collection<Session> sessions = sessionDAO.getActiveSessions();
    for (Session session : sessions) {
      UserOnline userOnline = new UserOnline();
      if(session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY) == null) {
        continue;
      } else {
        SimplePrincipalCollection principalCollection = (SimplePrincipalCollection) session
            .getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
        User userDO = (User) principalCollection.getPrimaryPrincipal();
        userOnline.setLoginName(userDO.getLoginName());
      }
      UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
      // 获取客户端操作系统
      String os = userAgent.getOperatingSystem().getName();
      // 获取客户端浏览器
      String browser = userAgent.getBrowser().getName();
      userOnline.setBrowser(browser);
      userOnline.setOs(os);
      User user = userMapper.selectUserByLoginName(userOnline.getLoginName());
      userOnline.setSessionId((String) session.getId());
      userOnline.setDeptName(user.getDept().getDeptName());
      if ("0:0:0:0:0:0:0:1".equals(session.getHost())){
        userOnline.setIpaddr("127.0.0.1");
      }
      String addr = AddressUtils.getRealAddressByIP(session.getHost());
      userOnline.setLonginLocation(addr);
      userOnline.setStartTimestamp(session.getStartTimestamp());
      userOnline.setExpireTime(session.getTimeout());
      list.add(userOnline);
    }
    return list;
  }

  @Override
  public List<User> listOnlineUser() {
    List<User> list = new ArrayList<>();
    User userDO;
    Collection<Session> sessions = sessionDAO.getActiveSessions();
    for (Session session : sessions) {
      SimplePrincipalCollection principalCollection = new SimplePrincipalCollection();
      if(session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY) == null) {
        continue;
      } else {
        principalCollection = (SimplePrincipalCollection) session
            .getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
        userDO = (User) principalCollection.getPrimaryPrincipal();
        list.add(userDO);
      }
    }
    return list;
  }

  @Override
  public Collection<Session> sessionList() {
    return sessionDAO.getActiveSessions();
  }

  @Override
  public boolean forceLogout(String sessionId) {
    Session session = sessionDAO.readSession(sessionId);
    session.setTimeout(0);
    sessionDAO.delete(session);
    return true;
  }
}
